package com.mashibing.mashibing;

import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * ScheduledPool定时任务线程池，就是我们原来学过一个定时器任务，隔段时间之后这个任务会执行
 *
 * 我们newScheduldThreadPool 的时候他返回的是 ScheduledThreadPoolExecutor,
 * 然后在ScheduledTheadPolExecutor 里面他调用了super，
 * 他的super又是ThreadPoolExecutor, 它本质上还是ThreadPoolExecutor。
 */
public class T_ScheduledPool {

    public static void main(String[] args) {
        ScheduledExecutorService service = Executors.newScheduledThreadPool(4);
        service.scheduleAtFixedRate(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(new Random().nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
        }, 0, 500, TimeUnit.MILLISECONDS);

    }
}
